﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KardexSist.UserControls.Common;
using Kardex_Entity;
using Kardex_BussinesLogic;
using Kardex_Common;
using KardexSist.Library;

namespace KardexSist.UserControls.Client
{
    public partial class CtrlClientDataPersonal : UserControl,  IOperations
    {
        private person PersonCurrent;
        private ClientFactory ClientFc;
        #region LanzarEventos
        public delegate void MyDelegadoBuscarEventHandler();
        public event MyDelegadoBuscarEventHandler MostrarListaClick;

        protected virtual void OnMostrarListaClick()
        {
            if (MostrarListaClick != null)
            { MostrarListaClick(); }
        }

        #endregion              
        public CtrlClientDataPersonal()
        {
            InitializeComponent();
            PersonCurrent = new person();
            if (!this.DesignMode)
            {
                LoadCmb();
            }
        }
        private void CtrlClientDataPersonal_Load(object sender, EventArgs e)
        {
            OnMostrarListaClick();
        }
        public void TrigerObjPerson(person objPerson)
        {
            PersonCurrent = new person() { client = new client() };
            PersonCurrent = objPerson;
            LoadObjectPeson();
        }
        private void LoadCmb()
        {
            try
            {  
                TypeClientFactory TypeClientActual = new TypeClientFactory();
                 TypeStatusCivilFactory TypeStatusCivilActual = new TypeStatusCivilFactory();
                 LugarCIexpedidoFactory LugarCI = new LugarCIexpedidoFactory();

                var ListTypeClient = TypeClientActual.GetListAll();
                cmbTypeClient.DataSource = ListTypeClient;
                cmbTypeClient.DisplayMember = "NameTypeClient";

                var ListTypeStatuCivil = TypeStatusCivilActual.GetListAll();
                cmbStateCivil.DataSource = ListTypeStatuCivil;
                cmbStateCivil.DisplayMember = "NameTypeStatusCivil";

                var ListLugarCI = LugarCI.GetListAll();
                cmbCIExpedido.DataSource = ListLugarCI;
                cmbCIExpedido.DisplayMember = "NamePlace";
                //cmbCIExpedido.ValueMember = "NamePlaceIdTipoCIExpedido";

            }
            catch (Exception ex)
            {

                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }
      
       
        private void LoadObjectPeson()
        {
            try
            {                
                _txtCD.Text = PersonCurrent.IdCD;
                txtCI.Text = PersonCurrent.CI;
                txtNombreFull.Text = PersonCurrent.Name;
                txtTelefono.Text = PersonCurrent.Telephone;
                cmbTypeClient.Text = PersonCurrent.client.NameTypeClient;
                cmbCIExpedido.Text = PersonCurrent.IdTipoCIExpedido;
                _dateBirth.Value = PersonCurrent.client.DateBirth.Value;
                cmbStateCivil.Text = PersonCurrent.client.NameTypeStatusCivil;
                txtDirection.Text = PersonCurrent.client.DirectionProp;
                txtRefDirection.Text = PersonCurrent.client.RefDirectionProp;

                if (PersonCurrent.Activ == true)
                {
                  _checkActivo.Checked = true;
                }
                else
                {
                    _checkActivo.Checked = false;
                }
            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }

        private void LoadData()
        {
            PersonCurrent.IdCD = _txtCD.Text;            
            PersonCurrent.Name = txtNombreFull.Text;            
            PersonCurrent.CI = txtCI.Text;
            PersonCurrent.IdTipoCIExpedido = ((lugarciexpedido) cmbCIExpedido.SelectedItem).IdTipoCIExpedido;
            PersonCurrent.client.DateBirth = _dateBirth.Value;
            PersonCurrent.client.NameTypeClient = cmbTypeClient.Text;
            PersonCurrent.client.NameTypeStatusCivil = cmbStateCivil.Text;            
            PersonCurrent.Telephone = txtTelefono.Text;
            PersonCurrent.client.DirectionProp = txtDirection.Text;
            PersonCurrent.client.RefDirectionProp = txtRefDirection.Text;
            PersonCurrent.Activ = _checkActivo.Checked;
            
        }
        private void ClearData()
        {
            //_numericIdPersona.Value = 0;
            _txtCD.Text = string.Empty;
            txtCI.Text = string.Empty;            
            txtNombreFull.Text = string.Empty;            
            txtTelefono.Text = string.Empty;
            _checkActivo.Checked = false;
        }

        public void Add()
        {
            ClearData();
            PersonCurrent = new person() { client = new client() };
        }

        public void Delete()
        {
            try
            {
                var result = Util.MessageBoxShow(Util.Enum_TypeMessage.QuestionDelete, null);
                if (result == DialogResult.Yes)
                {
                    ClientFc = new ClientFactory();
                    if (ClientFc.Delete(PersonCurrent) == true)
                    {
                        OnMostrarListaClick();
                        Util.MessageBoxShow(Util.Enum_TypeMessage.Delete, null);
                    }
                    else
                    {
                        Util.MessageBoxShow(Util.Enum_TypeMessage.Error, null);
                    }
                }
            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }

        public void Save()
        {
            try
            {
                //insert
                if (PersonCurrent.IdPerson <= 0)
                {

                    PersonCurrent = new person() { client = new client() };
                    LoadData();
                    ClientFc = new ClientFactory();
                    PersonCurrent = ClientFc.Save(PersonCurrent); 
                    if (PersonCurrent.IdPerson > 0)
                    {
                        OnMostrarListaClick();
                        Util.MessageBoxShow(Util.Enum_TypeMessage.Insert, null);
                    }
                    else
                    {
                        Util.MessageBoxShow(Util.Enum_TypeMessage.Error, null);
                    }
                }
                else
                {
                    var result = Util.MessageBoxShow(Util.Enum_TypeMessage.QuestionModif, null);
                    if (result == DialogResult.Yes)
                    {

                        LoadData();
                        ClientFc = new ClientFactory();
                        if (ClientFc.Update(PersonCurrent) == true)
                        {
                            OnMostrarListaClick();
                            Util.MessageBoxShow(Util.Enum_TypeMessage.Update, null);
                        }
                        else
                        {
                            Util.MessageBoxShow(Util.Enum_TypeMessage.Error, null);
                        }
                    }
                    else
                    {                        
                        ClearData();
                        LoadObjectPeson();
                    }
                }

            }
            catch (Exception ex)
            {
                Util.MessageBoxShow(Util.Enum_TypeMessage.ErrorExecption, ex);
            }
        }

        public void Export()
        {
            
        }
    }
}
